package com.itheima.controller;

import com.itheima.R;
import com.itheima.dto.CreateCouponDto;
import com.itheima.pojo.SmsFlashPromotion;
import com.itheima.service.FlashPromotionService;
import com.itheima.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Hakuna Matata
 * @date 2022-10-12-17:29
 */
@RestController
@RequestMapping("flash")
public class FlashPromotionController {
    @Autowired
    private FlashPromotionService flashPromotionService;

    //添加活动
    @PostMapping("create")
    public R flashPromotionCreate(@RequestBody SmsFlashPromotion smsFlashPromotion) {
        flashPromotionService.flashPromotionCreate(smsFlashPromotion);
        return R.success("秒杀活动增加成功!");
    }

    //删除活动
    @PostMapping("/delete/{id}")
    public R flashPromotionDelete(@PathVariable("id") Long id) {
        flashPromotionService.flashPromotionDelete(id);
        return R.success("删除活动成功");
    }

    //根据活动名称分页查询
    @GetMapping("list")
    public R getPromotionByPage(String keyword,
                                @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                @RequestParam(value = "pageSize", defaultValue = "10", required = true) Integer pageSize) {

        PageResult pageResult = flashPromotionService.getPromotionByPage(keyword, pageNum, pageSize);
        return R.success(pageResult);
    }

    //修改上下线状态
    @PostMapping("/update/status/{id}")
    public R updateStatus(@PathVariable("id") Long id, Integer status) {
        flashPromotionService.updateStatus(id, status);
        return R.success("操作成功");
    }

    //编辑活动
    @PostMapping("update/{id}")
    public R updatePromotion(@PathVariable("id") Long id, @RequestBody SmsFlashPromotion flashPromotion) {
        flashPromotionService.updatePromotion(id, flashPromotion);
        return R.success("编辑活动成功");
    }

    //获取活动详情
    @GetMapping("{id}")
    public R updateCoupon(@PathVariable("id") Long id) {
        SmsFlashPromotion flashPromotion = flashPromotionService.findPromotionById(id);
        return R.success(flashPromotion);
    }
}
